package com.lunwen.lunwen.services.impl;

import com.lunwen.lunwen.bean.DaoShi;
import com.lunwen.lunwen.bean.SysUserRole;
import com.lunwen.lunwen.mapper.DaoShiMapper;
import com.lunwen.lunwen.mapper.SysUserMapper;
import com.lunwen.lunwen.mapper.SysUserRoleMapper;
import com.lunwen.lunwen.services.DaoShiService;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
 * (DaoShi)表服务实现类
 *
 * @author makejava
 * @since 2020-07-20 12:25:07
 */
@Service
public class DaoShiServiceImpl implements DaoShiService {

    @Autowired
    private DaoShiMapper daoShiMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public DaoShi queryById(String id) {
        return this.daoShiMapper.queryById(id);
    }

    @Override
    public DaoShi queryByUserId(Long userId) {
        return this.daoShiMapper.queryByUserId(userId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<DaoShi> queryAllByLimit(DaoShi daoShi, int offset, int limit) {
        return this.daoShiMapper.queryAllByLimit(   daoShi.getXueyuanId(),
                                                    daoShi.getZhuanyeId(),
                                                    daoShi.getZhicheng(),
                                                    daoShi.getZhiwu(),
                                                    daoShi.getLeixing(),
                                                    offset,
                                                    limit);
    }

    /**
     * 新增数据
     *
     * @param daoShi 实例对象
     * @return 实例对象
     */
    @Override
    public DaoShi insert(DaoShi daoShi) {
        this.sysUserMapper.insert(daoShi.getSysUser());
        daoShi.setUserId(this.sysUserMapper.queryByName(daoShi.getSysUser().getUsername()).getUserId());
        this.daoShiMapper.insert(daoShi);
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setRoleId(2L);
        sysUserRole.setUserId(daoShi.getUserId());
        sysUserRoleMapper.insert(sysUserRole);
        return daoShi;
    }

    /**
     * 修改数据
     *
     * @param daoShi 实例对象
     * @return 实例对象
     */
    @Override
    public DaoShi update(DaoShi daoShi) {
        daoShi.getSysUser().setUserId(daoShi.getUserId());
        this.sysUserMapper.update(daoShi.getSysUser());
        this.daoShiMapper.update(daoShi);
        return this.queryByUserId(daoShi.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.daoShiMapper.deleteById(id) > 0;
    }

    @Override
    public int queryCount(DaoShi daoShi) {
        return this.daoShiMapper.queryCount(daoShi);
    }
}